<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_CONFIG
	'获取某个参数
	Public Property Get [Get](byval key)
		if isObject( POP_MVC.getConfig(key) ) Then
			set [Get] = POP_MVC.getConfig(key)
		else 
			[Get] = POP_MVC.getConfig(key)
		End If
	End Property	
	
	Public Property Set [Set]( byval key, byref val )
		POP_MVC.dict.edit config,key,val
	End Property
	
	'引入配置文件
	Public Function import_config ( config_file )
		if POP_MVC.fso.FileExists( POP_MVC.realPath(config_file)) Then
			set POP_MVC.config = POP_MVC.dict.Merge( POP_MVC.config,get_config(config_file) )
			import_config = true
			Easp.Debug = (not is_empty(C_("APP_DEBUG")))
		else
			import_config = false
		end If
	End Function
	
	'配置参数必须以'开头，如果想对某行进行说明，请在上一行用''(两个')开头
	Public Function get_config( config_file )		
		on error resume next
		dim lines,i,line,lineArr,bool,j,k,a,dictName,s
		dim config
		set config = D_ ' obj obj_name
		lines = split( POP_MVC.file_get_contents(config_file) ,vbCr)
		
		lines = POP_MVC.Arr.merge( lines,  split( POP_MVC.file_get_contents(config_file) ,vbLf) )
		
		for i = 0 to ubound(lines)	'循环每一行数据
			line = lines(i)	'每一行的内容
			line = trim(line)
			If mid(line,1,1) = "'" AND mid(line,2,1)<> "'" Then	'只解析以单个'开头的行
				line = mid(line, inStr(line,"'") + 1 )	'略去第一个字符'
				lineArr = split( line , ":" ,2 )	'用:将一行内容炸开成两个项
				If  ubound(lineArr) = 1 Then	'如果炸开的数组包含两个字符串才解析
					lineArr(0) = trim(lineArr(0))
					
					if inStr( lineArr(0) , "." ) > 0 Then
						a = split( lineArr(0) , "." )
						dictName = "config"
						for k = 0 to ubound(a)-1
							Execute "bool = " & dictName & ".Exists(""" & a(k) & """)"
							dictName = dictName & "(""" & a(k) & """)"
							if not bool Then								
								Execute "set " & dictName & " = Server.CreateObject(""Scripting.Dictionary"") "
							End If								
						next
						Execute dictName & "(""" & a(k) & """) = " & lineArr(1)
					Else	
						lineArr(1) = trim(lineArr(1))						
						Execute "config(""" & lineArr(0) & """) = " & lineArr(1)
					End If
				End if
			End if
		next
		
		set get_config = config
		call L_("POPASP_CONFIG.get_config")
	End Function
	
	' 将配置写入文件中
	' file : 配置文件，不应为框架的配置文件
	' attr : 属性，可为String与可为Dictionary，如果为后者，第三个参数不起作用
	' value : 属性值，只有在attr为String时才起作用。如果为null，则表示删除该属性
	Function Write( file,attr,value )
		on error resume next
		dim content,key,val,replaceWith,i
		i = 0
		content = POP_MVC.file_get_contents( file )
		if typename( attr ) = "Dictionary" then	'类型为Dictionary，则键值对代表了属性与值
			for each key in attr
				val = attr(key)
				if isNull(val) Then		'值为null时，将从配置文件中删除该属性
					replaceWith = "" & vbCrLf
				else
					val = Replace(val, """" , """""" )
					
					replaceWith = "'" & key & " : " & """" & val & """" & vbCrLf
				end if
				
				i = i + 1
				if POP_MVC.String.reg_test(content,"^'" & key & "\s*:.+","im") then
					content = POP_MVC.String.reg_replace( content,replaceWith,"^'" & key & "\s*:.+","im" )
				else 
					content = Replace( content, "%" & ">" , replaceWith & vbCrLf & "%" & ">" )
				end if
			next
		elseif typename(attr) = "String" then	'类型为String时
			if attr <> "" then
				if isNull( value ) Then			'值为null时，将从配置文件中删除该属性
					replaceWith = ""  & vbCrLf
				else 
					value = Replace(value, """" , """""" )
					replaceWith = "'" & attr & " : " & """" & value & """" & vbCrLf
				end if
				
				i = i + 1
				
				if POP_MVC.String.reg_test( content,"^'" & attr & "\s*:.+","im" ) then	
					content = POP_MVC.String.reg_replace( content,replaceWith,"^'" & attr & "\s*:.+","im" )
				else 
					content = Replace( content, "%" & ">" , replaceWith & vbCrLf & "%" & ">" )
				end if			
			end If
		else	
		end if
		content = POP_MVC.String.reg_replace( content,vbCrLf,"^\s*\n","gim" )
		call POP_MVC.file_put_contents( file,content )
		Write = i
		Call L_("POPASP_CONFIG.Write")
	end Function	
	
End Class
%>